package com.example.bishe.toolclass;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import sun.net.www.protocol.https.HttpsURLConnectionImpl;

import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author yws
 * @Date 2022/3/9 20:59
 */
@Component
public class ApiReturnDate {//根据URL接受到的返回JSON数据

    @Resource
    StringTurnJson stringTurnJson;

    public void Mydates(String Url) {
        JSONObject json = new JSONObject();
        try {
            URL url = new URL(Url);
            HttpsURLConnection huconn = (HttpsURLConnection) url.openConnection();
            huconn.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject Mydate(String Url) {
        JSONObject json = new JSONObject();
        try {
            URL url = new URL(Url);
            HttpsURLConnection huconn = (HttpsURLConnection) url.openConnection();
            BufferedReader in = null;
            StringBuilder result = new StringBuilder();
            huconn.connect();
            in = new BufferedReader(new InputStreamReader(huconn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            if (in != null) {
                in.close();
            }
            json = stringTurnJson.getJson(String.valueOf(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public JSONObject Mydate(String Url, String jsonObject) {
        JSONObject json = new JSONObject();
        try {
            URL url = new URL(Url);
            HttpsURLConnectionImpl huconn = (HttpsURLConnectionImpl) url.openConnection();
            huconn.setRequestMethod("POST");
            huconn.setRequestProperty("Content-Type", "application/json");
            huconn.setDoOutput(true);
            huconn.setDoInput(true);
            /*Set<String> set = new HashSet();
            set = map.keySet();
            for (String str: set) {
                huconn.setRequestProperty(str,map.get(str));
                System.out.println(str+" "+map.get(str));
            }*/
            BufferedReader in = null;
            OutputStream os = null;
            StringBuilder result = new StringBuilder();
            huconn.connect();
            os = huconn.getOutputStream();
            os.write(jsonObject.getBytes(StandardCharsets.UTF_8));
            os.flush();
            in = new BufferedReader(new InputStreamReader(huconn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            if (in != null) {
                in.close();
            }
            json = stringTurnJson.getJson(String.valueOf(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

}
